package com.ruoyi.project.system.controller;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.project.system.domain.*;
import com.ruoyi.project.system.service.ILaborSubcontractService;
import com.ruoyi.project.system.service.IProfessionalSubcontractingService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.service.IBasicInformationService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 基本信息Controller
 * 
 * @author ruoyi
 * @date 2021-10-15
 */
@RestController
@RequestMapping("/system/information")
public class BasicInformationController extends BaseController
{
    @Autowired
    private IBasicInformationService basicInformationService;

    @Autowired
    private IProfessionalSubcontractingService professionalSubcontractingService;

    @Autowired
    private ILaborSubcontractService laborSubcontractService;

    /**
     * 查询基本信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BasicInformation basicInformation)
    {
        startPage();
        List<BasicInformation> list = basicInformationService.selectBasicInformationList(basicInformation);
        for(BasicInformation information : list){
            ProfessionalSubcontracting professionalSubcontracting = professionalSubcontractingService.selectProfessionalSubcontractingById(Long.valueOf(information.getProfessionalSubcontractingId()));
            LaborSubcontract laborSubcontract = laborSubcontractService.selectLaborSubcontractById(Long.valueOf(information.getLaborSubcontractId()));
            if(professionalSubcontracting!=null){
                information.setProfessionalSubcontractingId(professionalSubcontracting.getName());
            }
            if(laborSubcontract!=null){
                information.setLaborSubcontractName(laborSubcontract.getName());
            }
            information.setRoleKey(SecurityUtils.getLoginUser().getUser().getRoles().get(0).getRoleKey());
        }
        return getDataTable(list);
    }

    /**
     * 导出基本信息列表
     */
    @Log(title = "基本信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BasicInformation basicInformation)
    {
        List<BasicInformation> list = basicInformationService.selectBasicInformationList(basicInformation);
        for(BasicInformation information : list){
            ProfessionalSubcontracting professionalSubcontracting = professionalSubcontractingService.selectProfessionalSubcontractingById(Long.valueOf(information.getProfessionalSubcontractingId()));
            LaborSubcontract laborSubcontract = laborSubcontractService.selectLaborSubcontractById(Long.valueOf(information.getLaborSubcontractId()));
            information.setProfessionalSubcontractingId(professionalSubcontracting.getName());
            information.setLaborSubcontractName(laborSubcontract.getName());
        }
        ExcelUtil<BasicInformation> util = new ExcelUtil<BasicInformation>(BasicInformation.class);
        return util.exportExcel(list, "基本信息数据");
    }

    /**
     * 获取基本信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(basicInformationService.selectBasicInformationById(id));
    }

    /**
     * 新增基本信息
     */
    @Log(title = "基本信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BasicInformation basicInformation)
    {
        basicInformation.setStatus("待审核");
        return toAjax(basicInformationService.insertBasicInformation(basicInformation));
    }

    /**
     * 修改基本信息
     */
    @Log(title = "基本信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BasicInformation basicInformation)
    {
        return toAjax(basicInformationService.updateBasicInformation(basicInformation));
    }

    /**
     * 删除基本信息
     */
    @Log(title = "基本信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(basicInformationService.deleteBasicInformationByIds(ids));
    }

    @GetMapping("/subcontractList")
    public AjaxResult subcontractList(){
        List<LaborSubcontract> laborSubcontracts = laborSubcontractService.selectLaborSubcontractList(null);
        return AjaxResult.success(laborSubcontracts);
    }

    @GetMapping("/subcontractingList")
    public AjaxResult subcontractingList(){
        List<ProfessionalSubcontracting> professionalSubcontractings = professionalSubcontractingService.selectProfessionalSubcontractingList(null);
        return AjaxResult.success(professionalSubcontractings);
    }

    @PostMapping("/submitAgain")
    public AjaxResult submitAgain(@RequestBody BasicInformation basicInformation)
    {
        basicInformation.setStatus("待审核");
        basicInformation.setRemark("");
        return toAjax(basicInformationService.updateBasicInformation(basicInformation));
    }

    @PostMapping("/reject")
    public AjaxResult reject(@RequestBody BasicInformation basicInformation)
    {
        basicInformation.setStatus("审核失败");
        return toAjax(basicInformationService.updateBasicInformation(basicInformation));
    }


    @PostMapping("/agree")
    public AjaxResult agree(@RequestBody BasicInformation basicInformation)
    {
        basicInformation.setStatus("审核成功");
        basicInformation.setRemark("");
        return toAjax(basicInformationService.updateBasicInformation(basicInformation));
    }
}
